package com.china08.yxyapi.api.manage.par.office.classnotice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeClass;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyNoticeClassRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 家长 班级通知 家长已读
 * 
 */
@CrossOrigin
@RestController("ParClassnoticeReadSrhController")
public class ParClassnoticeReadSrhController extends YxyApiBaseController {

    @Autowired
    private YxyNoticeClassRepository yxyNoticeClassRepository;

    @RequestMapping(value = "/api_yw/manage/par/myclass/classnotice/read/srh", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestParam(value = "childId", required = false) String childId, //
            @RequestParam(value = "childIds[]", required = false) String[] arrayChildIds,
            @RequestParam(value = "page", defaultValue = "0") int pageNum, //
            @RequestParam(value = "size", defaultValue = "10") int size) {
        Pageable pageable = new PageRequest(pageNum, size, Direction.DESC, "createdDate");
        int isRead = 1;
        List<YxyNoticeClass> page = null;
        Integer totalElements = 0;
        // 当前查询信息的孩子id列表
        if (StringUtils.isBlank(childId)) {// 查询全部孩子的已读信息
            List<String> childIds = new ArrayList<String>();
            if (arrayChildIds != null) {
                childIds = Arrays.asList(arrayChildIds);
            }
            page = yxyNoticeClassRepository.findByReceiverUserIdInAndReceiverIsRead(childIds, isRead, pageable);
            totalElements = yxyNoticeClassRepository.countByReceiverUserIdInAndReceiverIsRead(childIds, isRead);
        } else {// 查询某个孩子的已读信息
            // 家长收件箱
            page = yxyNoticeClassRepository.findByReceiverUserIdAndReceiverIsRead(childId, isRead, pageable);
            totalElements = yxyNoticeClassRepository.countByReceiverUserIdAndReceiverIsRead(childId, isRead);
        }
        List<RespModel> respModelLst = new ArrayList<RespModel>();
        RespModel respModel = null;
        for (YxyNoticeClass yxyNoticeClass : page) {
            respModel = new RespModel();
            respModel.id = yxyNoticeClass.getId();
            respModel.userId = yxyNoticeClass.getUserId();
            respModel.title = yxyNoticeClass.getTitle();
            respModel.content = yxyNoticeClass.getContent();
            respModel.createdDate = DateToolUtils.format(yxyNoticeClass.getCreatedDate(), "yyyy-MM-dd HH:mm");
            String avater = yxyNoticeClass.getAvatar();
            // 没有头像
            if (StringUtils.isBlank(avater)) {
                avater = "https://yxyosc.91yunxiao.com/yxyosc/2017/06/05/7a60f54be40b0020f2ad69899a250e8e.png";
            }
            // 不是完整图片
            if (!avater.startsWith("http")) {
                avater = "https://yxyosc.91yunxiao.com/yxyosc/" + avater;
            }
            respModel.avatar = avater;
            respModel.nickName = yxyNoticeClass.getNickName();
            respModel.image = yxyNoticeClass.getImage();
            respModel.attachmentName = yxyNoticeClass.getAttachmentName();
            respModel.attachmentPath = yxyNoticeClass.getAttachmentPath();
            respModelLst.add(respModel);
        }
        Page<RespModel> respModelPage = new PageImpl<RespModel>(respModelLst, pageable, totalElements);
        return respModelPage;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 消息id
        public String id;
        // 用户id
        public String userId;
        // 老师头像
        public String avatar;
        // 老师名称
        public String nickName;
        // 标题
        public String title;
        // 内容
        public String content;
        // 创建时间
        public String createdDate;
        // 图片列表
        public List<String> image;
        // 文件名称
        public String attachmentName;
        // 文件路劲
        public String attachmentPath;
    }
}
